package dao;

import bean.User;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;

import java.util.List;

/**
 * @ClassName : UserDao
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/4/17 11:15
 * @Description :
 */

public class UserDao {
    private static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Test
    public void test() {
//        System.out.println(login("root", "123456"));
//        System.out.println(getUserInfoById("1"));
        System.out.println(UserIconUpdate("1", "hello"));
    }

    public int UserMoneyCharge(User user) {

        try {

            String sql = "UPDATE user SET money=? WHERE id=?";

            return template.update(sql, user.getMoney(), user.getId());


        } catch (Exception e) {
            e.printStackTrace();
        }


        return 0;
    }

    public int UserIconUpdate(String user_id, String icon) {

        try {
            String sql = "UPDATE user SET icon=? WHERE id=?";

            return  template.update(sql, icon, user_id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public User getUserInfoById(String user_id) {

        try {
            String sql = "SELECT * FROM user WHERE id=?";
            return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user_id);


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public int userMoneyCost(String user_id, String cost) {

        try {
            String sql1 = "SELECT * FROM user WHERE id=?";
            User user = template.queryForObject(sql1, new BeanPropertyRowMapper<User>(User.class), user_id);

            String sql2 = "UPDATE user SET money=? WHERE id=?";
            double c = Double.parseDouble(cost);
            double money = Double.parseDouble(user.getMoney());
            money -= c;
            user.setMoney("" + money);

            return template.update(sql2, user.getMoney(), user.getId());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public static User login(String username, String psw) {
            String sql = "SELECT * FROM user WHERE username=? AND psw=?";
            List<User> users = template.query(sql, new BeanPropertyRowMapper<>(User.class), username, psw);
            if (!users.isEmpty()) {
                return users.get(0); // 返回第一个匹配的用户
            }
        return null;
    }

    public static int enroll(User user){
        String sql="INSERT INTO user(nickname,username,psw) VALUES (?, ?, ?)";
        int i = template.update(sql, user.getNickname(),user.getUsername(),user.getPsw());
        return i;
    }

    public static boolean getByUsername(String username){
        User user=null;
        try {
            String sql="SELECT * FROM user WHERE username=?";
            user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),username);
        }catch (Exception e){
        }
        if (user==null) {
            return true;
        }else {
            return false;
        }
    }


    public static List<User> getAllUser(){
        String sql = "Select * from user";
        List<User> user = template.query(sql, new BeanPropertyRowMapper<>(User.class));
        return user;
    }

    public static int insertUser(User user){
        String sql = "INSERT INTO user(nickname, username, psw, email) VALUES (?, ?, ?, ?)";
        int update = template.update(sql, user.getNickname(), user.getUsername(), user.getPsw(), user.getEmail());
        return update;
    }

    public static int delForUserName(String username) {
        String sql = "delete from user where username = ?";
        int update = template.update(sql, username);
        return update;
    }

    public static int updUserForUserName(User user) {
        String sql = "UPDATE user SET nickname=?, username=?, psw=?, email=?, money=? WHERE username=?";
        int update = template.update(sql, user.getNickname(), user.getUsername(),
                user.getPsw(), user.getEmail(), user.getMoney(), user.getUsername());
        return update;
    }

    public static List<String> getAllUsername() {

        String sql = "select username from user";
        List<String> query = template.queryForList(sql, String.class);
        return query;

    }

    public static String getIdByUsername(String username) {
        String sql = "SELECT id FROM user WHERE username=?";
        // 使用queryForObject方法执行查询，并将结果转换为整数类型
        Integer integer = template.queryForObject(sql, Integer.class, username);
        return integer.toString();
    }

    public static String getUsernameById(String id) {
        String sql = "select username from user where id = ?";
        String integer = template.queryForObject(sql, String.class, id);
        return integer;
    }
}
